package com.loveling.service.impl;

import com.loveling.entity.RedisDemoInfo;
import com.loveling.repository.RedisDemoInfoRepository;
import com.loveling.service.RedisDemoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * User: liangjiefa
 * Date: 2017/6/12 23:15
 * To change this template use File | Settings | File Templates.
 */
@Service
public class RedisDemoInfoServiceImpl implements RedisDemoInfoService {

    @Autowired
    private RedisDemoInfoRepository redisDemoInfoRepository;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Cacheable(value = "redisDemoInfo")
    @Override
    public RedisDemoInfo findById(long id) {
        System.err.println("DemoInfoServiceImpl.findById()=========从数据库中进行获取的....id=" + id);
        return redisDemoInfoRepository.findOne(id);
    }

    @Cacheable(value = "redisDemoInfo")
    @Override
    public void deleteFromCache(long id) {
        System.out.println("DemoInfoServiceImpl.delete().从缓存中删除.");
    }

    @Override
    public void test() {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set("mykey", "radom=" + Math.random());
        System.out.println(valueOperations.get("mykey"));
    }
}
